package com.aiju.cash.ui.good;

import android.os.Bundle;
import android.view.View;

import com.aiju.app.SimpleHttpCallback;
import com.aiju.base.activity.BaseTitleActivity;
import com.aiju.cash.R;
import com.aiju.cash.core.api.StockApi;
import com.aiju.cash.ui.stockupdate.activity.StockListHomePageActivity;
import com.aiju.utils.T;

/**
 * @author: 佐为
 * @date: 2017-07-04 15:33
 * @version: V1.0
 * @Description: 商品管理
 */
public class GoodManagerActivity extends BaseTitleActivity implements View.OnClickListener {

    private String status;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_good_manager);
        setCenterTitle("商品管理");

        findViewById(R.id.rl_good_manager).setOnClickListener(this);
        findViewById(R.id.rl_repertory_manager).setOnClickListener(this);
        findViewById(R.id.rl_auto_sync).setOnClickListener(this);
        findViewById(R.id.rl_hand_sync).setOnClickListener(this);
        if (!mUser.getShop().isOpenSynchronize()) {
            findViewById(R.id.rl_hand_sync).setVisibility(View.VISIBLE);
        } else {
            status = mUser.getShop().getIsOpenSynchronize();
            findViewById(R.id.rl_auto_sync).setSelected(true);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_good_manager:
                startActivity(GoodActivity.class);
                break;
            case R.id.rl_repertory_manager:
                startActivity(StockListHomePageActivity.class);
                break;
            case R.id.rl_auto_sync:
                if (v.isSelected()) {
                    status = "0";
                } else {
                    status = "1";
                }
                openSynchronize(v);
                break;
            case R.id.rl_hand_sync:
                synchronize();
                break;
        }
    }

    /**
     * 开启或者关闭同步功能
     * @param v
     */
    private void openSynchronize(final View v) {
        new StockApi(mDialogHelper).openSynchronize(status, new SimpleHttpCallback() {
            @Override
            protected void onDataSuccess(String data) {
                mUser.getShop().setIsOpenSynchronize(status);
                if (status.equals("1")) {
                    v.setSelected(true);
                    findViewById(R.id.rl_hand_sync).setVisibility(View.GONE);
                    T.ToastShort("同步功能已开启");
                } else {
                    v.setSelected(false);
                    findViewById(R.id.rl_hand_sync).setVisibility(View.VISIBLE);
                    T.ToastShort("同步功能已关闭");
                }
            }
        });
    }

    /**
     * 进行同步功能
     */
    private void synchronize() {
        new StockApi(mDialogHelper).synchronize(new SimpleHttpCallback() {
            @Override
            protected void onDataSuccess(String data) {
                T.ToastShort("商品已同步");
            }
        });
    }
}
